<?php
namespace App\Http\Controllers\Api\v1\Everyday;

use App\Http\Controllers\Controller;
use DB;


/**
 * 每日运势六项活跃分数
 * Class Active
 * @package app\everyday\controller
 */
class Active_devController extends Controller
{
    // 行运数据
    public $transitsData = [];
    // 三限数据
    public $thirdprogressedData = [];
    // 整宫制本命盘数据
    public $natalData = [];
    // 相位系数
    private $phase_modulus = [0=>1,180=>0.8,90=>0.75,120=>0.7,60=>0.6];
    // 初始化宫位分数
    private $transits_house_score;
    private $thirdprogressed_house_score;
    // 初始化行运（流年）日月水金火木分数
    private $transits_palent_score;
    // 初始化三限日月水金火木分数
    private $thirdprogressed_palent_score;
    // 行运月亮 不计行运月亮活跃
    private $tr_moon_not_tr_score = 0;

    /**
     * @return array
     */
    public function init()
    {
        $this->transits_house_score = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0];
        $this->thirdprogressed_house_score = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0];
        $this->transits_palent_score = [0,0,0,0,0,0];
        $this->thirdprogressed_palent_score = [0,0,0,0,0,0];
        $this->tr_moon_not_tr_score = 0;


        // 计算宫位分数
        $this->getHouseScore();
        // 计算行运星体活跃-本命星
        $this->transitsNatalPhase();
        // 计算行运星体活跃-行运星
        $this->transitsTransitsPhase();
        // 计算三限星体活跃
        $this->getThirdprogressedPlanetActiveScore();

        $money = $this->getMoneyScore();
        $work = $this->getWorkScore();
        $health = $this->getHealthScore();
        $study = $this->getStudyScore();
        $love = $this->getLoveScore();
        $talk = $this->getTalkScore();
        $travel = $this->getTravelScore();
        // TODO 家庭分太低了 暂不处理
        $parent = $this->getParentScore();

//        echo "流年宫位活跃：";
//        print_r($this->transits_house_score);
//        echo "三限宫位活跃：";
//        print_r($this->thirdprogressed_house_score);
//        echo "流年行星活跃：";
//        print_r($this->transits_palent_score);
//        echo "三限行星活跃：";
//        print_r($this->thirdprogressed_palent_score);

//        echo "财富：$money     (2宫+8宫+金星)/3\n<br/>";
//        echo "工作：$work      (10宫+6宫+太阳)/3\n<br/>";
//        echo "健康：$health    (6+8+1)/3\n<br/>";
//        echo "学业：$study    (三限水星活跃+3宫9宫活跃之最大值+流年水星活跃+流年3宫9宫活跃之最大值)/2\n<br/>";
//        echo "婚恋：$love     (5+7+金+火/2)/3.5\n<br/>";
//        echo "交际：$talk    (7宫+11宫+金星+水星/2+3宫/2)/4\n<br/>";
//        echo "出行：$travel   (3宫+9宫+水星+木星/2)/3.5\n<br/>";
//        echo "父母：$parent     (4宫+月亮)/2\n<br/>";
//        $data = [
//            '财富'=>$money,
//            '工作'=>$work,
//            '健康'=>$health,
//            '学习'=>$study,
//            '婚恋'=>$love,
//            '交际'=>$talk,
//            '出行'=>$travel,
//            '父母'=>$parent,
//        ];

        $money *= 2;
        $work *= 2;
        $health *= 2;
        $study *= 2;
        $love *= 2;
        $talk *= 2;
        $travel *= 2;
        $parent *= 2;

        $data = [
            'money'=>$money,
            'work'=>$work,
            'health'=>$health,
            'study'=>$study,
            'love'=>$love,
            'talk'=>$talk,
            'travel'=>$travel,
            'family'=>$parent,
        ];

        return $data;
    }
    /**
     * 获取财富活跃分
     */
    protected function getMoneyScore()
    {
        // (2宫+8宫+金星)/3
        // tr2与tr8取最大数，tp2与tp8取最大数，相加为财宫活跃；      （财宫活跃+金星）/2
//        $house2 = $this->transits_house_score[2] + $this->thirdprogressed_house_score[2];
//        $house8 = $this->transits_house_score[8] + $this->thirdprogressed_house_score[8];
        $planet3 = $this->transits_palent_score[3] + $this->thirdprogressed_palent_score[3];

//        $score = ($house2 + $house8 + $planet3) / 3;
        $tr_max = $this->transits_house_score[2] > $this->transits_house_score[8] ? $this->transits_house_score[2] : $this->transits_house_score[8];
        $tp_max = $this->thirdprogressed_house_score[2] > $this->thirdprogressed_house_score[8] ? $this->thirdprogressed_house_score[2] : $this->thirdprogressed_house_score[8];
        $score = ($tr_max + $tp_max + $planet3) / 2;

        return $score;
    }
    /**
     * 获取工作活跃分数
     */
    protected function getWorkScore()
    {
        // (10宫+6宫+太阳)/3
        // tr6与tr10取最大，tp6与tp10取最大，相加为事业宫活跃； （事业宫活跃+太阳+11宫活跃/3）/2；  若当前tp月亮在6/10宫或与6/10宫星体成相或两度内冲合中天，最终指数+10
        $house10 = $this->transits_house_score[10] + $this->thirdprogressed_house_score[10];
        $house6 = $this->transits_house_score[6] + $this->thirdprogressed_house_score[6];
        $planet0 = $this->transits_palent_score[0] + $this->thirdprogressed_palent_score[0];

//        $score = ($house10 + $house6 + $planet0) / 3;

        $tr_max = $this->transits_house_score[6] > $this->transits_house_score[10] ? $this->transits_house_score[6] : $this->transits_house_score[10];
        $tp_max = $this->thirdprogressed_house_score[6] > $this->thirdprogressed_house_score[10] ? $this->thirdprogressed_house_score[6] : $this->thirdprogressed_house_score[10];
        $house11 = $this->transits_house_score[11] + $this->thirdprogressed_house_score[11];

        $score = ($tr_max + $tp_max + $planet0 + $house11/3) / 2;

        $tp_moon = $this->thirdprogressedData['planets'][1];// 三限月亮

        // 若当前tp月亮在6/10宫或与6/10宫星体成相或两度内冲合中天，最终指数+10
        $con = false;

        if(in_array($tp_moon['in_house_id'],[6,10])) $con = true;

        if(!$con && !empty($tp_moon['phase']))
        {
            foreach ($tp_moon['phase'] as $phase)
            {
                if(!$con && in_array($this->thirdprogressedData['planets'][$phase['id']]['in_house_id'],[6,10])) $con = true;
                if(!$con && $phase['id'] == MC && $phase['phase_orb'] < 2 && in_array($phase['phase_e'],[0,180])) $con = true;
            }
        }
        if($con) $score += 10;

        return $score;
    }
    /**
     * 获取健康活跃分数
     */
    protected function getHealthScore()
    {
        // （6+8+1）/3
        // tr6/tr8取大，tp6/tp8取大，相加为疾病活跃；如本命盘6/8宫有星体且相互成相，或日月命主落6宫内被火土天海冥之一刑冲（整宫或普拉西度均生效），则，当三限月亮与海/土/火合刑冲时分别+15/10/7
//        $house6 = $this->transits_house_score[6] + $this->thirdprogressed_house_score[6];
//        $house8 = $this->transits_house_score[8] + $this->thirdprogressed_house_score[8];
//        $house1 = $this->transits_house_score[1] + $this->thirdprogressed_house_score[1];

//        $score = ($house6 + $house8 + $house1) / 3;

        $tr_max = $this->transits_house_score[6] > $this->transits_house_score[8] ? $this->transits_house_score[8] : $this->transits_house_score[8];
        $tp_max = $this->thirdprogressed_house_score[6] > $this->thirdprogressed_house_score[8] ? $this->thirdprogressed_house_score[6] : $this->thirdprogressed_house_score[8];

        $score = ($tr_max + $tp_max);

        $na_planet = $this->transitsData[0]['planets'];
        $na_houses = $this->transitsData[0]['houses'];
        $na_w_planet = $this->natalData['planets'];
        $na_w_houses = $this->natalData['houses'];

        // 如本命盘6/8宫有星体且相互成相，或日月命主落6宫内被火土天海冥之一刑冲（整宫或普拉西度均生效），则，当三限月亮与海/土/火合刑冲时分别+15/10/7
        $con = false;
        $na_protected = config('astrolog.protect_ancient_id')[$na_houses[1]['in_sign_id'] - 1];// 命主
        // 普拉西度
        if(!empty($na_houses[6]['in_house_area_planet_info']))
        {
            foreach ($na_houses[6]['in_house_area_planet_info'] as $pid)
            {
//                var_dump($pid);
                if(!$con && !empty($na_planet[$pid['id']]['phase']))
                {
                    foreach ($na_planet[$pid['id']]['phase'] as $phase)
                    {
                        if(!$con && $na_planet[$phase['id']]['in_house_id'] == 8) $con = true;
                        if(!$con && (
                            ($pid['id'] == 0 && (
                                (isset($na_planet[0]['phase']['0-4']) && in_array($na_planet[0]['phase']['0-4']['phase_e'],[90,180]))
                                || (isset($na_planet[0]['phase']['0-6']) && in_array($na_planet[0]['phase']['0-6']['phase_e'],[90,180]))
                                || (isset($na_planet[0]['phase']['0-7']) && in_array($na_planet[0]['phase']['0-7']['phase_e'],[90,180]))
                                || (isset($na_planet[0]['phase']['0-8']) && in_array($na_planet[0]['phase']['0-8']['phase_e'],[90,180]))
                                || (isset($na_planet[0]['phase']['0-9']) && in_array($na_planet[0]['phase']['0-9']['phase_e'],[90,180]))))
                            ||
                            ($pid['id'] == 1 && (
                                    (isset($na_planet[1]['phase']['1-4']) && in_array($na_planet[1]['phase']['1-4']['phase_e'],[90,180]))
                                    || (isset($na_planet[1]['phase']['1-6']) && in_array($na_planet[1]['phase']['1-6']['phase_e'],[90,180]))
                                    || (isset($na_planet[1]['phase']['1-7']) && in_array($na_planet[1]['phase']['1-7']['phase_e'],[90,180]))
                                    || (isset($na_planet[1]['phase']['1-8']) && in_array($na_planet[1]['phase']['1-8']['phase_e'],[90,180]))
                                    || (isset($na_planet[1]['phase']['1-9']) && in_array($na_planet[1]['phase']['1-9']['phase_e'],[90,180]))))
                            ||
                            ($pid['id'] == $na_protected && (
                                    (isset($na_planet[$na_protected]['phase'][$na_protected.'-4']) && in_array($na_planet[$na_protected]['phase'][$na_protected.'-4']['phase_e'],[90,180]))
                                    || (isset($na_planet[$na_protected]['phase'][$na_protected.'-6']) && in_array($na_planet[$na_protected]['phase'][$na_protected.'-6']['phase_e'],[90,180]))
                                    || (isset($na_planet[$na_protected]['phase'][$na_protected.'-7']) && in_array($na_planet[$na_protected]['phase'][$na_protected.'-7']['phase_e'],[90,180]))
                                    || (isset($na_planet[$na_protected]['phase'][$na_protected.'-8']) && in_array($na_planet[$na_protected]['phase'][$na_protected.'-8']['phase_e'],[90,180]))
                                    || (isset($na_planet[$na_protected]['phase'][$na_protected.'-9']) && in_array($na_planet[$na_protected]['phase'][$na_protected.'-9']['phase_e'],[90,180]))))
                            )
                        ) $con = true;
                    }
                }
            }
        }
        // 整宫制
        if(!$con && !empty($na_w_houses[6]['in_house_area_planet_info']))
        {
            foreach ($na_w_houses[6]['in_house_area_planet_info'] as $pid)
            {
                if(!$con && !empty($na_w_planet[$pid['id']]['phase']))
                {
                    foreach ($na_w_planet[$pid['id']]['phase'] as $phase)
                    {
                        if(!$con && $na_w_planet[$phase['id']]['in_house_id'] == 8) $con = true;
                        if(!$con && (
                            ($pid['id'] == 0 && (
                                (isset($na_w_planet[0]['phase']['0-4']) && in_array($na_w_planet[0]['phase']['0-4']['phase_e'],[90,180]))
                                || (isset($na_w_planet[0]['phase']['0-6']) && in_array($na_w_planet[0]['phase']['0-6']['phase_e'],[90,180]))
                                || (isset($na_w_planet[0]['phase']['0-7']) && in_array($na_w_planet[0]['phase']['0-7']['phase_e'],[90,180]))
                                || (isset($na_w_planet[0]['phase']['0-8']) && in_array($na_w_planet[0]['phase']['0-8']['phase_e'],[90,180]))
                                || (isset($na_w_planet[0]['phase']['0-9']) && in_array($na_w_planet[0]['phase']['0-9']['phase_e'],[90,180]))))
                            ||
                            ($pid['id'] == 1 && (
                                    (isset($na_w_planet[1]['phase']['1-4']) && in_array($na_w_planet[1]['phase']['1-4']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[1]['phase']['1-6']) && in_array($na_w_planet[1]['phase']['1-6']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[1]['phase']['1-7']) && in_array($na_w_planet[1]['phase']['1-7']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[1]['phase']['1-8']) && in_array($na_w_planet[1]['phase']['1-8']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[1]['phase']['1-9']) && in_array($na_w_planet[1]['phase']['1-9']['phase_e'],[90,180]))))
                            ||
                            ($pid['id'] == $na_protected && (
                                    (isset($na_w_planet[$na_protected]['phase'][$na_protected.'-4']) && in_array($na_w_planet[$na_protected]['phase'][$na_protected.'-4']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[$na_protected]['phase'][$na_protected.'-6']) && in_array($na_w_planet[$na_protected]['phase'][$na_protected.'-6']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[$na_protected]['phase'][$na_protected.'-7']) && in_array($na_w_planet[$na_protected]['phase'][$na_protected.'-7']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[$na_protected]['phase'][$na_protected.'-8']) && in_array($na_w_planet[$na_protected]['phase'][$na_protected.'-8']['phase_e'],[90,180]))
                                    || (isset($na_w_planet[$na_protected]['phase'][$na_protected.'-9']) && in_array($na_w_planet[$na_protected]['phase'][$na_protected.'-9']['phase_e'],[90,180]))))
                            )
                        ) $con = true;
                    }
                }
            }
        }
        if($con === true)
        {
            $tp_moon = $this->thirdprogressedData['planets'][1];
            if(isset($tp_moon['phase']['1-8']) && in_array($tp_moon['phase']['1-8']['phase_e'],[0,90,180])) $score += 15;
            if(isset($tp_moon['phase']['1-6']) && in_array($tp_moon['phase']['1-6']['phase_e'],[0,90,180])) $score += 10;
            if(isset($tp_moon['phase']['1-4']) && in_array($tp_moon['phase']['1-4']['phase_e'],[0,90,180])) $score += 7;
        }

        return $score;
    }
    /**
     * 获取学业活跃分数
     */
    protected function getStudyScore()
    {
        // （三限水星活跃+3宫9宫活跃之最大值+流年水星活跃+流年3宫9宫活跃之最大值）/2
        // （三限水星活跃+3宫9宫活跃之最大值+流年水星活跃+流年3宫9宫活跃之最大值）/2 ；   tr日金水与本命木星成非刑相+10，tp水木成非刑相+10【修正添加】
        $house3 = $this->transits_house_score[3] + $this->thirdprogressed_house_score[3];
        $house9 = $this->transits_house_score[9] + $this->thirdprogressed_house_score[9];
        $tr_house3 = $this->transits_house_score[3];// 流年3宫
        $tr_house9 = $this->transits_house_score[9];// 流年9宫
        $planet3 = $this->thirdprogressed_palent_score[3] + $this->transits_palent_score[3];

        $max3_9 = ($house3 > $house9) ? $house3 : $house9;
        $tr_max3_9 = ($tr_house3 > $tr_house9) ? $tr_house3 : $tr_house9;

        $score = ($planet3 + $max3_9 + $tr_max3_9) / 2;


        // tr日金水与本命木星成非刑相+10，tp水木成非刑相+10【修正添加】
        $tr_planet = $this->transitsData[1]['planets'];
        $tp_planet = $this->thirdprogressedData['planets'];
        if(
            (isset($tr_planet[0]['phase']['0-5']) && $tr_planet[0]['phase']['0-5']['phase_e'] != 90)
            || (isset($tr_planet[2]['phase']['2-5']) && $tr_planet[2]['phase']['2-5']['phase_e'] != 90)
            || (isset($tr_planet[3]['phase']['3-5']) && $tr_planet[3]['phase']['3-5']['phase_e'] != 90)
        )
        {
            $score += 10;
        }
        if((isset($tp_planet[2]['phase']['2-5']) && $tp_planet[2]['phase']['2-5']['phase_e'] != 90))
        {
            $score += 10;
        }

        return $score;
    }
    /**
     * 获取婚恋活跃分数
     */
    protected function getLoveScore()
    {
        // （5+7+金+火/2）/3.5
        // tr5/tr7取大，tp5/tp7取大，相加为婚恋宫活跃；   （婚恋宫活跃+金星）/2
//        $house5 = $this->transits_house_score[5] + $this->thirdprogressed_house_score[5];
//        $house7 = $this->transits_house_score[7] + $this->thirdprogressed_house_score[7];
        $planet3 = $this->thirdprogressed_palent_score[3] + $this->transits_palent_score[3];
        $planet4 = $this->thirdprogressed_palent_score[4] + $this->transits_palent_score[4];

//        $score = ($house5 + $house7 + $planet3 + $planet4/4) / 3.5;

        $tr_max = $this->transits_house_score[5] > $this->transits_house_score[7] ? $this->transits_house_score[5] : $this->transits_house_score[7];
        $tp_max = $this->thirdprogressed_house_score[5] > $this->thirdprogressed_house_score[7] ? $this->thirdprogressed_house_score[5] : $this->thirdprogressed_house_score[7];

        $score = ($tr_max + $tp_max + $planet3) / 2;

        return $score;
    }
    /**
     * 获取交际活跃分数
     */
    protected function getTalkScore()
    {
        // （7宫+11宫+金星+水星/2+3宫/2）/4
        // tr7/tr11取大，tp7/tp11取大，相加为交际宫活跃； （交际宫活跃+金星活跃+水星/2+3宫/2）/2
        $house11 = $this->transits_house_score[11] + $this->thirdprogressed_house_score[11];
        $house7 = $this->transits_house_score[7] + $this->thirdprogressed_house_score[7];
        $house3 = $this->transits_house_score[3] + $this->thirdprogressed_house_score[3];
        $planet3 = $this->thirdprogressed_palent_score[3] + $this->transits_palent_score[3];
        $planet2 = $this->thirdprogressed_palent_score[2] + $this->transits_palent_score[2];

//        $score = ($house7 + $house11 + $planet3 + $planet2/2 + $house3/2) / 4;
        $tr_max = $this->transits_house_score[7] > $this->transits_house_score[11] ? $this->transits_house_score[7] : $this->transits_house_score[11];
        $tp_max = $this->thirdprogressed_house_score[7] > $this->thirdprogressed_house_score[11] ? $this->thirdprogressed_house_score[7] : $this->thirdprogressed_house_score[11];

        $score = ($tr_max + $tp_max + $planet3 + $planet2 / 2 + $house3 / 2) / 2;


        return $score;
    }
    /**
     * 获取出行活跃分数
     */
    protected function getTravelScore()
    {
        // （3宫+9宫+水星+木星/2）/3.5
        // tr3/tr9取大，tp3/tp9取大，相加得出行宫活跃; (出行宫活跃+水星+木星/2)/2.3
        $house9 = $this->transits_house_score[9] + $this->thirdprogressed_house_score[9];
        $house3 = $this->transits_house_score[3] + $this->thirdprogressed_house_score[3];
        $planet5 = $this->thirdprogressed_palent_score[5] + $this->transits_palent_score[5];
        $planet2 = $this->thirdprogressed_palent_score[2] + $this->transits_palent_score[2];

//        $score = ($house3 + $house9 + $planet2 + $planet5/2) / 3.5;
        $tr_max = $this->transits_house_score[3] > $this->transits_house_score[9] ? $this->transits_house_score[3] : $this->transits_house_score[9];
        $tp_max = $this->thirdprogressed_house_score[3] > $this->thirdprogressed_house_score[9] ? $this->thirdprogressed_house_score[3] : $this->thirdprogressed_house_score[9];

        $score = ($tr_max + $tp_max + $planet2 + $planet5 / 2) / 2;

        return $score;
    }
    /**
     * 获取父母活跃分数
     */
    protected function getParentScore()
    {
        // （4宫+月亮）/2
        // (tr月亮活跃不计算行运月亮)/1.8;     此处应注意！！！计算行运月亮活跃时，只计算《计算本命星体之某星》，不计算《计算行运星体之某星》。
//        $house4 = $this->transits_house_score[4] + $this->thirdprogressed_house_score[4];
//        $planet1 = $this->transits_palent_score[1] + $this->thirdprogressed_house_score[1];
//
//        $score = ($house4 + $planet1 ) / 2;

        $score = $this->tr_moon_not_tr_score / 1.8;

        return $score;
    }
    /**
     * 计算宫位得分
     */
    protected function getHouseScore()
    {
        $this->transitsPlanetInHouse();
        $this->getThirdprogressedHouseActiveScore();
        $this->transitsTransitsPhaseHouse();
        $this->getThirdprogressedMoonPhaseHouse();
    }
    /**
     * 行运星落宫
     */
    private function transitsPlanetInHouse()
    {
        $transits_planets = $this->transitsData[1]['planets'];// 行运星
        foreach($transits_planets as $planet)
        {
            // 星体落入该宫位 仅限日月水金火木土天海冥
            if($planet['id'] <= 9)
            {
                $add = 0;
                switch($planet['id'])
                {
                    case 0:
                        $add = 5;
                        break;
                    case 1:
                        $add = 4;
                        break;
                    case 2:
                        $add = 4;
                        break;
                    case 3:
                        $add = 4;
                        break;
                    case 4:
                        $add = 4;
                        break;
                    case 5:
                        $add = 5;
                        break;
                    case 6:
                        $add = 3;
                        break;
                    case 7:
                        $add = 1;
                        break;
                    case 8:
                        $add = 1;
                        break;
                    case 9:
                        $add = 1;
                        break;
                }
                // 若该星当前停滞，分数翻倍
                if($this->isStay($planet['id'],$planet['speed'])) $add *= 2;

                $this->thirdprogressed_house_score[$planet['in_house_id']] += $add;
            }
        }
    }
    /**
     * 行运盘-本命星成相  本命星得分
     */
    private function transitsNatalPhase()
    {
        // 本命星数据
        $natal_planets = $this->transitsData[0]['planets'];

        foreach ($natal_planets as $planet)
        {
            // 本命星 日月水金火木
            if($planet['id'] > 5) continue;

            if(!empty($planet['phase']))
            {
                foreach ($planet['phase'] as $phase)
                {
                    // 初始化成相值
                    $n_p = 0;
                    // 成相的行运星需为 日月水金火木土天海冥
                    if($phase['id'] > 9) continue;
                    // 计算成相值
                    if($phase['id'] == 0)
                    {
                        $n_p = 7;
                        $level[$planet['id']][] = 2;
                    }
                    elseif($phase['id'] == 1)
                    {
                        $n_p = 3;
                        $level[$planet['id']][] = 3;
                    }
                    elseif($phase['id'] == 2)
                    {
                        $n_p = 4;
                        $level[$planet['id']][] = 2;
                    }
                    elseif($phase['id'] == 3)
                    {
                        $n_p = 4;
                        $level[$planet['id']][] = 2;
                    }
                    elseif($phase['id'] == 4)
                    {
                        $n_p = 4;
                        $level[$planet['id']][] = 2;
                    }
                    elseif($phase['id'] == 5)
                    {
                        $n_p = 2;
                        $level[$planet['id']][] = 1;
                    }
                    elseif($phase['id'] == 6)
                    {
                        $n_p = 1.5;
                        $level[$planet['id']][] = 1;
                    }
                    elseif($phase['id'] == 7)
                    {
                        $n_p = 1;
                        $level[$planet['id']][] = 0;
                    }
                    elseif($phase['id'] == 8)
                    {
                        $n_p = 1;
                        $level[$planet['id']][] = 0;
                    }
                    elseif($phase['id'] == 9)
                    {
                        $n_p = 1;
                        $level[$planet['id']][] = 0;
                    }
                    //  成相的行运星id
                    $transits_palent = $this->transitsData[1]['planets'][$phase['id']];
                    // 若该行运星当前停滞，得分额外+5
                    if($this->isStay($phase['id'],$transits_palent['speed']))
                    {
                        $n_p += 5;
                    }
                    // 相位系数
                    $phase_modulus = $this->phase_modulus[$phase['phase_e']];

                    $this->transits_palent_score[$planet['id']] += $n_p * $phase_modulus;
                    if($planet['id'] == 1) $this->tr_moon_not_tr_score += $n_p * $phase_modulus;
                }

                if(!empty($level[$planet['id']]))
                {
                    // 某星同时被0/1级激发+2
                    if(in_array(0,$level[$planet['id']]) && in_array(1,$level[$planet['id']]))
                    {
                        $this->transits_palent_score[$planet['id']] += 1;
                        if($planet['id'] == 1) $this->tr_moon_not_tr_score += 1;
                    }
                    // 某星同时被1/2级激发+5
                    if(in_array(1,$level[$planet['id']]) && in_array(2,$level[$planet['id']]))
                    {
                        $this->transits_palent_score[$planet['id']] += 5;
                        if($planet['id'] == 1) $this->tr_moon_not_tr_score += 5;
                    }
                    // 某星同时被0/2级激发+5
                    if(in_array(0,$level[$planet['id']]) && in_array(2,$level[$planet['id']]))
                    {
                        $this->transits_palent_score[$planet['id']] += 5;
                        if($planet['id'] == 1) $this->tr_moon_not_tr_score += 5;
                    }
                    // 某星同时被1/3级激发+2
                    if(in_array(1,$level[$planet['id']]) && in_array(3,$level[$planet['id']]))
                    {
                        $this->transits_palent_score[$planet['id']] += 2;
                        if($planet['id'] == 1) $this->tr_moon_not_tr_score += 2;
                    }
                    // 某星同时被2/3级激发+2
                    if(in_array(2,$level[$planet['id']]) && in_array(3,$level[$planet['id']]))
                    {
                        $this->transits_palent_score[$planet['id']] += 2;
                        if($planet['id'] == 1) $this->tr_moon_not_tr_score += 2;
                    }
                }
            }
        }
    }
    /**
     * 行运盘-行运星成相  行运星得分
     */
    private function transitsTransitsPhase()
    {
        // 行运星数据
        $transits_planets = $this->transitsData[1]['planets'];

        foreach ($transits_planets as $planet)
        {
            // 本命星 日月水金火木
            if($planet['id'] > 5) continue;

            if(!empty($planet['phase']))
            {
                // 初始化
                $phase_score[$planet['id']] = [
                    's'=>[],
                    'pm'=>[],
                ];
                foreach ($planet['phase'] as $phase)
                {
                    // 初始化成相值
                    $n_p = 0;
                    // 成相的行运星需为 日月水金火木土天海冥 或者合轴
                    if (($phase['id'] > 9 && $phase['id'] < ASC)
                        || ($phase['id'] >= ASC && !in_array($phase['phase_e'],[0,180]))) continue;
                    // 计算成相值
                    // 太阳
                    if ($phase['id'] == 0)
                    {
                        $n_p = 6;
                    }
                    // 月亮
                    elseif ($phase['id'] == 1)
                    {
                        $n_p = 6;
                    }
                    // 金水火
                    elseif ($phase['id'] == 2 || $phase['id'] == 3 || $phase['id'] == 4)
                    {
                        $n_p = 4;
                    }
                    // 木土
                    elseif ($phase['id'] == 5 || $phase['id'] == 6)
                    {
                        $n_p = 1;
                        // tr日月金水火与之成相额外 改为+3
                        if(isset($transits_planets[0]['phase']['0-'.$phase['id']])
                            || isset($transits_planets[1]['phase']['1-'.$phase['id']])
                            || isset($transits_planets[2]['phase']['2-'.$phase['id']])
                            || isset($transits_planets[3]['phase']['3-'.$phase['id']])
                            || isset($transits_planets[4]['phase']['4-'.$phase['id']]))
                        {
                            $n_p = 3;
                        }
                    }
                    // 天海冥
                    elseif ($phase['id'] == 7 || $phase['id'] == 8 || $phase['id'] == 9)
                    {
                        $n_p = 0.5;
                        // tr日月金水火与之成相改为+2.5
                        if(isset($transits_planets[0]['phase']['0-'.$phase['id']])
                            || isset($transits_planets[1]['phase']['1-'.$phase['id']])
                            || isset($transits_planets[2]['phase']['2-'.$phase['id']])
                            || isset($transits_planets[3]['phase']['3-'.$phase['id']])
                            || isset($transits_planets[4]['phase']['4-'.$phase['id']]))
                        {
                            $n_p = 2.5;
                        }
                    }
                    // 合轴
                    elseif($phase['id'] >= ASC && in_array($phase['phase_e'],[0,180]))
                    {
                        $n_p = 8;
                    }
                    //  成相的本命星id
                    $natal_palent = $this->transitsData[0]['planets'][$phase['id']];
                    // 若为古典命主
                    if($phase['id'] == config('astrolog.protect_ancient_id')[$natal_palent['in_sign_id'] - 1])
                    {
                        $n_p += 4;
                    }
                    // 若该本命星当前停滞
                    if ($this->isStay($phase['id'], $natal_palent['speed']))
                    {
                        $n_p *= 2;
                    }
                    // 相位系数
                    $phase_modulus = $this->phase_modulus[$phase['phase_e']];

                    $phase_score[$planet['id']]['s'][] = $n_p;
                    $phase_score[$planet['id']]['pm'][] = $phase_modulus;
                }
                if(isset($phase_score[$planet['id']]) &&count($phase_score[$planet['id']]['s']) > 0)
                {
                    // 最高分
                    $max = max($phase_score[$planet['id']]['s']);
                    // 最高分的key
                    $max_key = array_search($max,$phase_score[$planet['id']]['s']);

                    foreach ($phase_score[$planet['id']]['s'] as $k => $ps)
                    {
                        $p_m = $phase_score[$planet['id']]['pm'][$k];
                        if($k == $max_key)
                        {
                            $this->transits_palent_score[$planet['id']] += $ps * $p_m;
                        }
                        else
                        {
                            $this->transits_palent_score[$planet['id']] += $ps * 0.5 * $p_m;
                        }
                    }
                }
            }
        }
    }
    /**
     * 行运盘-行运星成相  本命宫位得分
     */
    private function transitsTransitsPhaseHouse()
    {
        // 行运星数据
        $transits_planets = $this->transitsData[1]['planets'];

        foreach ($transits_planets as $planet)
        {
            // 行运星 日月水金火木土天海冥
            if($planet['id'] > 9) continue;

            if(!empty($planet['phase']))
            {
                $t_p_in_house = $planet['in_house_id'];

                if($planet['id'] == 0)
                {
                    $t_p = 5;
                }
                elseif($planet['id'] == 1)
                {
                    $t_p = 4;
                }
                elseif($planet['id'] == 2)
                {
                    $t_p = 4;
                }
                elseif($planet['id'] == 3)
                {
                    $t_p = 4;
                }
                elseif($planet['id'] == 4)
                {
                    $t_p = 4;
                }
                elseif($planet['id'] == 5)
                {
                    $t_p = 5;
                }
                elseif($planet['id'] == 6)
                {
                    $t_p = 3;
                }
                elseif($planet['id'] == 7)
                {
                    $t_p = 1;
                }
                elseif($planet['id'] == 8)
                {
                    $t_p = 1;
                }
                elseif($planet['id'] == 9)
                {
                    $t_p = 1;
                }
                // 若停滞 分数翻倍
                if($this->isStay($planet['id'],$planet['speed']))
                {
                    $t_p *= 2;
                }
                foreach ($planet['phase'] as $phase)
                {
                    $n_p = 0;
                    // 成相的本命星需为 日月水金火木土天海冥
                    if($phase['id'] > 9) continue;

                    if($phase['id'] == 0)
                    {
                        $n_p = 5;
                    }
                    elseif($phase['id'] == 1)
                    {
                        $n_p = 5;
                    }
                    elseif($phase['id'] == 2)
                    {
                        $n_p = 3;
                    }
                    elseif($phase['id'] == 3)
                    {
                        $n_p = 3;
                    }
                    elseif($phase['id'] == 4)
                    {
                        $n_p = 3;
                    }
                    elseif($phase['id'] == 5)
                    {
                        $n_p = 1;
                    }
                    elseif($phase['id'] == 6)
                    {
                        $n_p = 0.5;
                    }
                    elseif($phase['id'] == 7)
                    {
                        $n_p = 0.5;
                    }
                    elseif($phase['id'] == 8)
                    {
                        $n_p = 0.5;
                    }
                    elseif($phase['id'] == 9)
                    {
                        $n_p = 0.5;
                    }
                    //  成相的本命星id
                    $natal_palent = $this->transitsData[0]['planets'][$phase['id']];
                    // 若为古典命主
                    if($phase['id'] == config('astrolog.protect_ancient_id')[$natal_palent['in_sign_id'] - 1])
                    {
                        $n_p += 2;
                    }
                    // 行运容许系数
                    // 30分内
                    if($phase['phase_orb'] < 0.5)
                    {
                        $orb_modulus = 1;
                    }
                    // 1度内
                    elseif($phase['phase_orb'] < 1)
                    {
                        $orb_modulus = 0.95;
                    }
                    // 1.5内
                    elseif($phase['phase_orb'] < 1.5)
                    {
                        $orb_modulus = 0.85;
                    }
                    // 2内
                    else
                    {
                        $orb_modulus = 0.7;
                    }
                    // 相位系数
                    $phase_modulus = $this->phase_modulus[$phase['phase_e']];

                    $add = ($t_p + $n_p) * $phase_modulus * $orb_modulus;

                    $n_p_in_house = $natal_palent['in_house_id'];
//                    echo "行运盘-行运星成相".$planet['id'].'-'.$phase['id']."  本命宫位".$n_p_in_house."得分 +$add <br/>";
//                    echo "行运盘-行运星成相".$planet['id'].'-'.$phase['id']."  行运星所在宫位".$t_p_in_house."得分 + ".$add/2 ."<br/>";
                    $this->transits_house_score[$n_p_in_house] += $add;
                    $this->transits_house_score[$t_p_in_house] += $add/2;
                }
            }
        }
    }
    /**
     * 获取三限宫位活跃分数
     */
    protected function getThirdprogressedHouseActiveScore()
    {
        // 三限行星数据
        $thirdprogressed_planet = $this->thirdprogressedData['planets'];

        foreach ($thirdprogressed_planet as $planet)
        {
            if($planet['id'] > 9) continue;

            $h_add = 0;

            if($planet['id'] == 0 || $planet['id'] == 2 || $planet['id'] == 3)
            {
                $h_add = 3;
            }
            elseif($planet['id'] == 1)
            {
                $h_add = 15;
            }
            elseif($planet['id'] == 4 || $planet['id'] == 5)
            {
                $h_add = 4;
            }
            elseif($planet['id'] == 6)
            {
                $h_add = 3;
            }
            elseif($planet['id'] == 7 || $planet['id'] == 8 || $planet['id'] == 9)
            {
                $h_add = 1.5;
            }
            $this->thirdprogressed_house_score[$planet['in_house_id']] += $h_add;
        }
    }
    /**
     * 三限宫位活跃-相位激发
     */
    protected function getThirdprogressedMoonPhaseHouse()
    {
        // 三限行星数据
        $thirdprogressed_planet = $this->thirdprogressedData['planets'];

        if(!empty($thirdprogressed_planet[1]['phase']))
        {
            $house_add = [];// 成相星宫位得分
            $house_priming = [];// 引动相位宫位得分
            foreach ($thirdprogressed_planet[1]['phase'] as $phase)
            {
                if($phase['id'] > 9 ) continue;

                // 30分内1
                if($phase['phase_orb'] < 0.5)
                {
                    $orb_modulus1 = 1;
                }
                // 30分-1度0.95
                elseif($phase['phase_orb'] < 1)
                {
                    $orb_modulus1 = 0.95;
                }
                // 1度-2度0.9
                elseif($phase['phase_orb'] < 2)
                {
                    $orb_modulus1 = 0.9;
                }
                // 2度-3度0.8
                elseif($phase['phase_orb'] <= 3)
                {
                    $orb_modulus1 = 0.8;
                }
                // 3度外0.7
                else
                {
                    $orb_modulus1 = 0.7;
                }
                // 该星所在宫位
                $in_house = $thirdprogressed_planet[$phase['id']]['in_house_id'];
                // 相位系数1
                $phase_modulus1 = $this->phase_modulus[$phase['phase_e']];
                // 月亮与某星成相时，该星所在宫位获得加分 10*相位系数*容许系数
                $house_add[$in_house][] = 10 * $phase_modulus1 * $orb_modulus1;

                if(!empty($thirdprogressed_planet[$phase['id']]['phase']))
                {
                    // 循环与该星成相的星体的所有相位
                    foreach ($thirdprogressed_planet[$phase['id']]['phase'] as $ps)
                    {
                        // 不与月亮成相
                        if(!isset($thirdprogressed_planet[1]['phase']['1-'.$ps['id']]))
                        {
                            // 星体宫位
                            $ps_in_house = $thirdprogressed_planet[$ps['id']]['in_house_id'];
                            // 相位容许系数
                            // 30分内1
                            if($ps['phase_orb'] < 0.5)
                            {
                                $orb_modulus2 = 1;
                            }
                            // 30分-1度0.95
                            elseif($ps['phase_orb'] < 1)
                            {
                                $orb_modulus2 = 0.95;
                            }
                            // 1度-2度0.9
                            elseif($ps['phase_orb'] < 2)
                            {
                                $orb_modulus2 = 0.9;
                            }
                            // 2度-3度0.8
                            elseif($ps['phase_orb'] <= 3)
                            {
                                $orb_modulus2 = 0.8;
                            }
                            // 3度外0.7
                            else
                            {
                                $orb_modulus2 = 0.7;
                            }
                            // 相位系数2
                            $phase_modulus2 = $this->phase_modulus[$ps['phase_e']];
                            // 8*相位系数1*容许系数1*相位系数2*容许系数2
                            $house_priming[$ps_in_house][] = 8 * $phase_modulus1 * $orb_modulus1 * $phase_modulus2 * $orb_modulus2;
                        }
                    }
                }
            }
            $h_add = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0,];
            if(!empty($house_add))
            {
                foreach ($house_add as $h1 => $ha)
                {
                    if(count($ha) > 0)
                    {
                        // 多颗星同宫时，最高分保留，每多一颗星+1
                        $h_add[$h1] += (max($ha) + (count($ha) - 1));
                    }
                }
            }
            if(!empty($house_priming))
            {
                foreach ($house_priming as $h2 => $hp)
                {
                    // 多颗星以相同方式引动，只计最高分
                    $h_add[$h2] += max($hp);
                }
            }
            if(!empty($h_add))
            {
                foreach ($h_add as $h => $s)
                {
                    // 三限宫位活跃（最终值*1.25）
                   $this->thirdprogressed_house_score[$h] += $s * 1.25;
                }
            }
        }
    }
    /**
     * 获取三限行星活跃分数
     */
    protected function getThirdprogressedPlanetActiveScore()
    {
        // 三限行星数据
        $thirdprogressed_planet = $this->thirdprogressedData['planets'];
        // 三限行星结构数据
        $structure = $this->thirdprogressedData['structure'];
//        print_r($structure);
        $p_add = [0,0,0,0,0,0];
        foreach ($thirdprogressed_planet as $planet)
        {
            if($planet['id'] > 5) continue;

            if(!empty($planet['phase']))
            {

                foreach ($planet['phase'] as $phase)
                {
                    // 三限容许系数
                    // 30分内1
                    if($phase['phase_orb'] < 0.5)
                    {
                        $orb_modulus1 = 1;
                    }
                    // 30分-1度0.95
                    elseif($phase['phase_orb'] < 1)
                    {
                        $orb_modulus1 = 0.95;
                    }
                    // 1度-2度0.9
                    elseif($phase['phase_orb'] < 2)
                    {
                        $orb_modulus1 = 0.9;
                    }
                    // 2度-3度0.8
                    elseif($phase['phase_orb'] <= 3)
                    {
                        $orb_modulus1 = 0.8;
                    }
                    // 3度外0.7
                    else
                    {
                        $orb_modulus1 = 0.7;
                    }
                    // 相位系数
                    $phase_modulus1 = $this->phase_modulus[$phase['phase_e']];
                    // 与月亮成相
                    if($phase['id'] == 1)
                    {
                        // 15*相位系数*容许系数
                        $p_add[$planet['id']] += 15 * $phase_modulus1 * $orb_modulus1;
                    }
                    if(!empty($thirdprogressed_planet[$phase['id']]['phase']))
                    {
                        // 循环与该星成相的星体的所有相位
                        foreach ($thirdprogressed_planet[$phase['id']]['phase'] as $ps)
                        {
                            // 不与月亮成相
                            if(!isset($thirdprogressed_planet[1]['phase']['1-'.$ps['id']]))
                            {
                                // 相位容许系数
                                // 30分内1
                                if($ps['phase_orb'] < 0.5)
                                {
                                    $orb_modulus2 = 1;
                                }
                                // 30分-1度0.95
                                elseif($ps['phase_orb'] < 1)
                                {
                                    $orb_modulus2 = 0.95;
                                }
                                // 1度-2度0.9
                                elseif($ps['phase_orb'] < 2)
                                {
                                    $orb_modulus2 = 0.9;
                                }
                                // 2度-3度0.8
                                elseif($ps['phase_orb'] <= 3)
                                {
                                    $orb_modulus2 = 0.8;
                                }
                                // 3度外0.7
                                else
                                {
                                    $orb_modulus2 = 0.7;
                                }
                                // 相位系数2
                                $phase_modulus2 = $this->phase_modulus[$ps['phase_e']];
                                // 15*相位系数1*相位系数2*容许1*容许2
                                $p_add[$planet['id']] = 15 * $phase_modulus1 * $orb_modulus1 * $phase_modulus2 * $orb_modulus2;
                            }
                        }
                    }
                    // 合轴10*容许系数
                    if($phase['id'] >= ASC && in_array($phase['phase_e'],[0,180]))
                    {
                        $p_add[$planet['id']] = 10 * $orb_modulus1;
                    }
                    // 与太阳成相
                    if($phase['id'] == 0)
                    {
                        // 7*相位系数*容许系数
                        $p_add[$planet['id']] += 7 * $phase_modulus1 * $orb_modulus1;
                    }
                }
                // 检测是否处于封闭结构中 与两颗以上星形成T三角等封闭结构+5 结构包含月亮+5 每多一颗星与之成相+1
                $is_in = false;
                $moon_is_in = false;
                if(!empty($structure['trine']))
                {
                    foreach ($structure['trine'] as $trine)
                    {
                        // 与两颗以上星形成T三角等封闭结构+5
                        if(in_array($planet['id'],$trine))
                        {
                            $is_in = true;
                            $p_add[$planet['id']] += 5;
                            if(count($planet['phase']) > 2)
                            {
                                $p_add[$planet['id']] += (count($planet['phase']) -2);
                            }
                            // 本身非月亮 结构包含月亮
                            if($planet['id'] != 1 && in_array(1,$trine))
                            {
                                $moon_is_in = true;
                                $p_add[$planet['id']] += 5;
                            }
                        }
                    }
                }
                // 小三角 small_triangle
                if(!empty($structure['small_triangle']))
                {
                    foreach ($structure['small_triangle'] as $small_triangle)
                    {
                        // 与两颗以上星形成T三角等封闭结构+5
                        if(!$is_in && in_array($planet['id'],$small_triangle))
                        {
                            $is_in = true;
                            $p_add[$planet['id']] += 5;
                            if(count($planet['phase']) > 2)
                            {
                                $p_add[$planet['id']] += (count($planet['phase']) -2);
                            }
                            // 本身非月亮 结构包含月亮
                            if(!$moon_is_in && $planet['id'] != 1
                                && in_array(1,$small_triangle))
                            {
                                $moon_is_in = true;
                                $p_add[$planet['id']] += 5;
                            }
                        }

                    }
                }
                // T三角 t_square
                if(!empty($structure['t_square']))
                {
                    foreach ($structure['t_square'] as $t_square)
                    {
                        // 与两颗以上星形成T三角等封闭结构+5
                        if(!$is_in && in_array($planet['id'],$t_square))
                        {
                            $is_in = true;
                            $p_add[$planet['id']] += 5;
                            if(count($planet['phase']) > 2)
                            {
                                $p_add[$planet['id']] += (count($planet['phase']) -2);
                            }
                            // 本身非月亮 结构包含月亮
                            if(!$moon_is_in && $planet['id'] != 1
                                && in_array(1,$t_square))
                            {
                                $moon_is_in = true;
                                $p_add[$planet['id']] += 5;
                            }
                        }

                    }
                }
                // 桥三角 bridge_triangle
                if(!empty($structure['bridge_triangle']))
                {

//                    var_dump($structure['bridge_triangle']);
                    foreach ($structure['bridge_triangle'] as $bridge_triangle)
                    {
                        // 与两颗以上星形成T三角等封闭结构+5
                        if(!$is_in && in_array($planet['id'],$bridge_triangle))
                        {
//                            echo $planet['id']."<br/>";
                            $p_add[$planet['id']] += 5;
                            if(count($planet['phase']) > 2)
                            {
                                $p_add[$planet['id']] += (count($planet['phase']) -2);
                            }
                            // 本身非月亮 结构包含月亮
                            if(!$moon_is_in && $planet['id'] != 1
                                && in_array(1,$bridge_triangle))
                            {
                                $p_add[$planet['id']] += 5;
                            }
//                            var_dump($p_add);
                        }

                    }
                }
            }
        }
        if(!empty($p_add))
        {
            foreach ($p_add as $p => $pa)
            {
//                echo $p ."------".$pa."<br/>";
                $this->thirdprogressed_palent_score[$p] += $pa * 5 / 3;
            }
        }
    }
    /**
     * 行星是否停滞
     *
     * @param integer $pid 行星id
     * @param float $speed 行星速度
     * @return boolean 停滞 true || 
     */
    protected function isStay(int $pid,float $speed)
    {
        if($pid > 9 || $pid < 2) return false;
        // 木星土星：一天小于一分
        // 冥王：小于0.4分
        // 水星：小于15分
        // 金星：小于12分
        // 火星：小于5分
        // 天王海王：小于0.5分
        if($pid == 2 && abs($speed) < 15/60)
        {
            return true;
        }
        elseif($pid == 3 && abs($speed) < 12/60)
        {
            return true;
        }
        elseif($pid == 4 && abs($speed) < 5/60)
        {
            return true;
        }
        elseif($pid == 5 && abs($speed) < 1/60)
        {
            return true;
        }
        elseif($pid == 6 && abs($speed) < 1/60)
        {
            return true;
        }
        elseif($pid == 7 && abs($speed) < 0.5/60)
        {
            return true;
        }
        elseif($pid == 8 && abs($speed) < 0.5/60)
        {
            return true;
        }
        elseif($pid == 9 && abs($speed) < 0.4/60)
        {
            return true;
        }
        return false;
    }
}